package com.hssy.wululuaplan.controller;

import com.hssy.wululuaplan.common.Result;
import com.hssy.wululuaplan.entity.InviteCodeRecord;
import com.hssy.wululuaplan.form.InviteCodeForm;
import com.hssy.wululuaplan.form.RegisterForm;
import com.hssy.wululuaplan.service.RegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 注册 控制器
 * @since 2025/6/19 17:06
 * @author Mr.Hongtao
 */
@Api(tags = "注册")
@RestController
@RequestMapping("/register")
public class RegisterController {
    @Resource
    private RegisterService registerService;

    @ApiOperation("注册")
    @PostMapping
    public Result<String> register(@RequestBody @Valid RegisterForm form) {
        ReentrantLock lock = new ReentrantLock();
        lock.lock();
        try {
            registerService.register(form);
        } finally {
            lock.unlock();
        }
        return Result.success("注册成功");
    }

    @ApiOperation("创建邀请码")
    @PostMapping("/createInviteCode")
    public Result<InviteCodeRecord> createInviteCode(@RequestHeader("Authorization") String token,
                                                     @RequestBody(required = false) InviteCodeForm form) {
        InviteCodeRecord inviteCode = registerService.createInviteCode(token, form.getExpiry());
        return Result.success(inviteCode);
    }

    @ApiOperation("查看验证码")
    @GetMapping("/getInviteCodeList")
    public Result<List<InviteCodeRecord>> getInviteCodeList(@RequestHeader("Authorization") String token) {
        List<InviteCodeRecord> list = registerService.getInviteCodeList(token);
        return Result.success(list);
    }
}
